package com.asiainfo.complier.format;

import com.asiainfo.complier.helper.StringHelper;
import java.util.Stack;

public class CodeLine {

	private int fromIndex;

	/**
	 * @return the fromIndex
	 */
	public int getFromIndex() {
		return fromIndex;
	}

	/**
	 * @param fromIndex
	 *            the fromIndex to set
	 */
	public void setFromIndex(int fromIndex) {
		this.fromIndex = fromIndex;
	}

	/**
	 * @return the endIndex
	 */
	public int getEndIndex() {
		return endIndex;
	}

	/**
	 * @param endIndex
	 *            the endIndex to set
	 */
	public void setEndIndex(int endIndex) {
		this.endIndex = endIndex;
	}

	private int endIndex;
	private int step;

	/**
	 * @return the step
	 */
	public int getStep() {
		return step;
	}

	/**
	 * @param step
	 *            the step to set
	 */
	public void setStep(int step) {
		this.step = step;
	}

	/**
	 * @return the line
	 */
	public String getLine() {
		return line;
	}

	/**
	 * @param line
	 *            the line to set
	 */
	public void setLine(String line) {
		this.line = line;
	}

	public CodeLine(int step, String line, int fromIndex, int endIndex) {

		this.step = step;
		this.line = line;
		this.fromIndex = fromIndex;
		this.endIndex = endIndex;
	}

	public CodeLine(int step, String line) {

		this.step = step;
		this.line = line;
	}

	private String line;

	public String filterEmpty() {
		if (StringHelper.isNullOrEmpty(line))
			return line;
		line = line.replace('\t', ' ');
		int size = line.length();
		Stack<Character> strStack = new Stack<Character>();
		String retString = "";
		for (int i = 0; i < size; i++) {
			char c = line.charAt(i);
			if (strStack.isEmpty()) {
				strStack.push(c);
				retString += c;
			} else {
				char lastChar = strStack.peek();
				if (c == ' ' && lastChar == ' ') {
					continue;
				} else {
					strStack.push(c);
					retString += c;
				}

			}
		}
		return retString;

	}

	public static void main(String[] args) {
		CodeLine l = new CodeLine(1, "ta  bb cc.             ttt");
		System.out.println(l.filterEmpty());
	}

	public static String EMPTY_STRING = "    ";

	/**
	 * @param steps
	 */
	public static String ToString(int steps) {
		String str = "";
		for (int i = 0; i < steps; i++) {
			str += EMPTY_STRING;
		}
		return str;
	}

	@Override
	public String toString() {

		String str = "";

		// System.out.println(step + "  -->" + line);
		for (int i = 0; i < step; i++) {
			str += EMPTY_STRING;
		}
		line = filterEmpty();
		str += line;
		str += "\r\n";
		return str;
	}

}
